Return to start page
Core/Maths/Struct Vector 3.j
1 library AStructCoreMathsVector3 requires ALibraryCoreMathsHandle, ALibraryCoreMathsPoint
2
3 struct AVector3
4 //dynamic members
5 private real m_x
6 private real m_y
7 private real m_z
8
9 //dynamic members
10
11 public method setX takes real x returns nothing
12 set this.m_x = x
13 endmethod
14
15 public method x takes nothing returns real
16 return this.m_x
17 endmethod
18
19 public method setY takes real y returns nothing
20 set this.m_y = y
21 endmethod
22
23 public method y takes nothing returns real
24 return this.m_y
25 endmethod
26
27 public method setZ takes real z returns nothing
28 set this.m_z = z
29 endmethod
30
31 public method z takes nothing returns real
32 return this.m_z
33 endmethod
34
35 //convenience methods
36
37 public method setWidget takes widget usedWidget returns nothing
38 set this.m_x = GetWidgetX(usedWidget)
39 set this.m_y = GetWidgetY(usedWidget)
40 set this.m_z = GetWidgetZ(usedWidget)
41 endmethod
42
43 public method setUnit takes unit usedUnit returns nothing
44 set this.m_x = GetUnitX(usedUnit)
45 set this.m_y = GetUnitY(usedUnit)
46 set this.m_z = GetUnitZ(usedUnit)
47 endmethod
48
49 //methods
50
51 public method setLength takes real length returns nothing
52 call this.scale((length / this.length()))
53 endmethod
54
55 public method length takes nothing returns real
56 return SquareRoot((this.m_x * this.m_x) + (this.m_y * this.m_y) + (this.m_z * this.m_z))
57 endmethod
58
59 /// Copys all data from vector @param vector.
60 public method copy takes thistype vector returns nothing
61 set this.m_x = vector.m_x
62 set this.m_y = vector.m_y
63 set this.m_z = vector.m_z
64 endmethod
65
66 /// Adds all data from vector @param vector.
67 /// Das Ergebnis ist ein Vektor, der vom Punkt 0 zur Spitze vom Vektor "this" verläuft, wenn man diesen an die Spitze des Vektors "vector" legt.
68 public method add takes thistype vector returns nothing
69 set this.m_x = this.m_x + vector.m_x
70 set this.m_y = this.m_y + vector.m_y
71 set this.m_z = this.m_z + vector.m_z
72 endmethod
73
74
75 /// Subtracts all data from vector @param vector.
76 /// Das Ergebnis ist ein Vektor, der von der Spitze des Vektors "vector" zur Spitze des Vektors "this" verläuft.
77 /// Achtung: Der Vektor verläuft immer vom 2. Wert zum 1. Wert der Subtraktion.
78 public method subtract takes thistype vector returns nothing
79 set this.m_x = this.m_x - vector.m_x
80 set this.m_y = this.m_y - vector.m_y
81 set this.m_z = this.m_z - vector.m_z
82 endmethod
83
84 /// Multiplies each value from vector @param vector and returns the sum of all results.
85 /// Ist das Ergebnis 0, so entsteht ein rechter Winkel, wenn man die beiden Vektoren mit ihren Spitzen aneinander legt.
86 public method multiply takes thistype vector returns real
87 return ((this.m_x * vector.m_x) + (this.m_y * vector.m_y) + (this.m_z * vector.m_z))
88 endmethod
89
90 /// Multiplies all values with the value of @param factor.
91 /// Ist der Betrag des Wertes grer oder gleich 1 oder kleiner als 0, so verlängert sich der Vektor.
92 /// Anderenfalls wird der Vektor kürzer.
93 public method scale takes real factor returns nothing
94 set this.m_x = this.m_x * factor
95 set this.m_y = this.m_y * factor
96 set this.m_z = this.m_z * factor
97 endmethod
98
99 /// Adds scaled vector @param vector which is scaled by value @param factor.
100 /// Note that vector @param vector won't be changed!
101 /// @author peq
102 public method addScaled takes thistype vector, real factor returns nothing
103 set this.m_x = this.m_x + vector.m_x * factor
104 set this.m_y = this.m_y + vector.m_y * factor
105 set this.m_z = this.m_z + vector.m_z * factor
106 endmethod
107
108 /// Projects vector @param vector.
109 /// Passt die Richtung des Vektors "this" an die Richtung des Vektors "vector" an ohne dabei die Länge des Vektors "this" zu verändern.
110 public method project takes thistype vector returns nothing
111 local real factor = this.length() / vector.length()
112 set this.m_x = vector.m_x * factor
113 set this.m_y = vector.m_y * factor
114 set this.m_z = vector.m_z * factor
115 endmethod
116
117 /// Rotates a vector through @param angle.
118 /// Only x and y will be changed. Rotation on z axis is not possible yet.
119 public method rotate takes real angle returns nothing
120 local real beta = (Asin(this.m_y / this.length()) - (angle * bj_DEGTORAD))
121 set this.m_x = this.length() * Cos(beta)
122 set this.m_y = this.length() * Sin(beta)
123 endmethod
124
125
126 // Gets the normal vector of the terrain at given coordinates. @param sampleRadius defines
127 // how far apart the reference points will be, if they are further apart, the result will
128 // be an impression of smoother terrain; normaly the value should be between 0 and 128.
129 /// @todo Not tested yet.
130 public method terrainNormal takes real x, real y, real sampleRadius returns nothing
131 local real array z
132 local thistype vectorX = thistype.create(0.0, 0.0, 0.0)
133 local thistype vectorY = thistype.create(0.0, 0.0, 0.0)
134 //Z
135 set z[0] = GetTerrainZ((x - sampleRadius), y)
136 set z[1] = GetTerrainZ((x + sampleRadius), y)
137 set z[2] = GetTerrainZ((y - sampleRadius), y)
138 set z[3] = GetTerrainZ((y + sampleRadius), y)
139 //Vector X
140 set vectorX.m_x = (2.0 * sampleRadius)
141 //set VectorX.Y = 0.00
142 set vectorX.m_z = (z[1] - z[0])
143 //Vector Y
144 //set VectorY.X = 0.00
145 set vectorY.m_y = (2.0 * sampleRadius)
146 set vectorY.m_z = (z[3] - z[2])
147 //Add
148 call vectorX.add(vectorY)
149 //Copy
150 call this.copy(vectorX)
151 //Destroy
152 call thistype.destroy(vectorX)
153 call thistype.destroy(vectorY)
154 endmethod
155
156 public static method create takes real x, real y, real z returns thistype
157 local thistype this = thistype.allocate()
158 //dynamic members
159 set this.m_x = x
160 set this.m_y = y
161 set this.m_z = z
162
163 return this
164 endmethod
165
166 /// Returns the product of the two vectors @param vector0 and @vector1 in a new vector instance.
167 public static method product takes thistype vector0, thistype vector1 returns thistype
168 local thistype result = thistype.create(0.0, 0.0, 0.0)
169 call result.copy(vector0)
170 call result.add(vector1)
171 return result
172 endmethod
173
174 /// Returns the difference of the two vectors @param vector0 and @vector1 in a new vector instance.
175 public static method difference takes thistype vector0, thistype vector1 returns thistype
176 local thistype result = thistype.create(0.0, 0.0, 0.0)
177 call result.copy(vector0)
178 call result.subtract(vector1)
179 return result
180 endmethod
181
182 /// Returns the multiplication of the two vectors @param vector0 and @vector1 in a new vector instance.
183 /// @author Tamino Dauth
184 /// @state untested
185 public method multiplication takes thistype vector0, thistype vector1 returns thistype
186 local thistype result = thistype.create(0.0, 0.0, 0.0)
187 call result.copy(vector0)
188 call result.multiply(vector1)
189 return result
190 endmethod
191
192 /// Returns the projection of the two vectors @param vector0 and @vector1 in a new vector instance.
193 /// @author Tamino Dauth
194 /// @state untested
195 public method projection takes thistype vector0, thistype vector1 returns thistype
196 local thistype result = thistype.create(0.0, 0.0, 0.0)
197 call result.copy(vector0)
198 call result.project(vector1)
199 return result
200 endmethod
201 endstruct
202
203 endlibrary